<?php

/**
 * 清单广场-推荐清单池（每天1点更新一次）
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1);
ini_set("error_reporting", E_ALL ^ E_NOTICE);

require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\Album\AlbumListModel;
use Doraemon\cache\AlbumCounter;
use hhz\constant\redis\redisKeys;
use Hhz\Redis\newCache;

$res = AlbumListModel::selectByWhere([
    'quality' => [
        AlbumListModel::QUALITY_GOOD, AlbumListModel::QUALITY_EXCELLENT
    ],
    'status' => AlbumListModel::STATS_LIST_PUBLIC_SHOW_MAP,
    'publish_time[<=]' => strtotime("-7 days"),
    'publish_time[>=]' => strtotime("-30 days"),
]);
if (empty($res) || count($res) < 6) {
    var_dump('未查询到符合条件到清单或则不足6个,当前' . count($res) . '个，查询7天以前的200条数据');
    $res = AlbumListModel::selectByWhere([
        'quality' => [
            AlbumListModel::QUALITY_GOOD, AlbumListModel::QUALITY_EXCELLENT
        ],
        'status' => AlbumListModel::STATS_LIST_PUBLIC_SHOW_MAP,
        'publish_time[<=]' => strtotime("-7 days"),
        'ORDER' => [
            'publish_time' => 'DESC'
        ],
        'LIMIT' => [0, 200]
    ]);
}
$albumIds = array_column($res, 'album_id');
var_dump('符合条件的清单id:' . json_encode($albumIds));
$albumCounter = AlbumCounter::getMulti($albumIds);

$albumList = [];

foreach ($res as $row) {
    $albumId = $row['album_id'];
    $row['join_num'] = $albumCounter[$albumId]['read'] ?? 0;
    $albumList[] = $row;
}
$sortJoinNum = array_column($albumList, 'join_num');
array_multisort($sortJoinNum, SORT_DESC, $albumList);

$recommendAlbumIds = [];
foreach ($albumList as $row) {
    if (count($recommendAlbumIds) > 30) {
        break;
    }
    $recommendAlbumIds[] = $row['album_id'];
}
var_dump('参与人数 top 30 清单id：' . json_encode($recommendAlbumIds));
if ($recommendAlbumIds) {
    $newRedis = new newCache(redisKeys::KEY_B_CACHE_ALBUM_SQUARE_RECOMMEND_POOL);
    $newRedis->setCache( implode(',', $recommendAlbumIds));
}

